using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using logistical.Models;

namespace logistical.Controllers
{
    public class sys_iflagController : baseController
    {
        public class list_sys_iflag
        {
            public List<sys_iflag> objs { get; set; }
        }

        [HttpGet]
        public object get()
        {
            try
            {
                var o = new logisticalEntities();
                var p = o.sys_iflag.OrderBy(x => new { x.typecode, x.code });

                return new
                {
                    data = p

                };
            }
            catch (Exception ex)
            {
                return new { msg = ex.Message };
            }
        }


        [HttpPost]
        public object add(sys_iflag obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                obj.id = Guid.NewGuid().ToString();
                o.sys_iflag.Add(obj);
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object edit(sys_iflag obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.sys_iflag.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.sys_iflag.SingleOrDefault(x => x.id == obj.id);
                    old.typecode = obj.typecode;
                    old.code = obj.code;
                    old.name = obj.name;
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object del(sys_iflag obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.sys_iflag.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.sys_iflag.SingleOrDefault(x => x.id == obj.id);
                    o.sys_iflag.Remove(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object mdel(list_sys_iflag objs)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                foreach (var p in objs.objs)
                {
                    var co = o.sys_iflag.Where(x => x.id == p.id);
                    if (co.Count() == 1)
                    {
                        var old = o.sys_iflag.SingleOrDefault(x => x.id == p.id);
                        o.sys_iflag.Remove(old);
                    }
                }
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }
    }
}
